import 'package:Flutter_Demo/core/model/category_model.dart';
import 'package:Flutter_Demo/core/services/json_parse.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:Flutter_Demo/core/extention/int_extention.dart';
import 'package:Flutter_Demo/core/extention/double_extention.dart';

import 'home_category_item.dart';


// 有局限性 比如上拉加载 下拉刷新
class MYHomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return FutureBuilder<List<MYCategoryModel>>(
      // 返回future
      future: MYJsonParse.getCategoryData(),
      builder: (ctx, snapshot) {
        // snapshot 判断是否有数据
        if(!snapshot.hasData) return Center(child: CircularProgressIndicator());
        if (snapshot.hasError) return Center(child: Text("请求失败了"),);

        final categories = snapshot.data;
        return GridView.builder(
          padding: EdgeInsets.all(20.0),
          itemCount: categories.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            // 2列
              crossAxisCount: 2,
              crossAxisSpacing: 20.0,
              mainAxisSpacing: 20.0,
              // 宽高比
              childAspectRatio: 1.5
          ),
          itemBuilder: (ctx, index) {
            return MYHomeCategoryItem(categories[index]);
          },
        );
      }
    );
  }
}



/*
class MYHomeContent extends StatefulWidget {
  @override
  _MYHomeContentState createState() => _MYHomeContentState();
}

class _MYHomeContentState extends State<MYHomeContent> {

  List<MYCategoryModel> _categories = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    // 加载数据
    MYJsonParse.getCategoryData().then((value) {
      _categories = value;
      print(value);
    });
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      padding: EdgeInsets.all(20.0),
      itemCount: _categories.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        // 2列
        crossAxisCount: 2,
        crossAxisSpacing: 20.0,
        mainAxisSpacing: 20.0,
        // 宽高比
        childAspectRatio: 1.5
      ),
      itemBuilder: (ctx, index) {
        return MYHomeCategoryItem(_categories[index]);
      },

    );
  }
}
 */


